<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/7/7 16:09
 */

namespace App\Project\Printer;

use App\Models\MedicalDispose;
use Illuminate\Support\Arr;

class DisposePrinter extends PrinterAbstract
{
    use MedicationFormat;

    /**
     * @var \App\Project\Scene\MedicalScene
     */
    protected $scene;

    public function __construct(MedicalDispose $dispose)
    {
        $this->model = $dispose;
        $this->scene = app('scene');
    }

    public function controlObjectives()
    {
        if (!$this->scene->isWestern()) {
            return null;
        }

        return $this->control();
    }

    protected function control()
    {
        $control = $this->model->control_objectives;

        if (!$control) {
            return null;
        }

        $print = [
            $this->fe('control_objectives', 'hba1c', 'HbA1c'),
            $this->fe('control_objectives', 'fbg', '空腹血糖'),
            $this->fe('control_objectives', '2hpbg', '餐后2h血糖'),

        ];

        if (isset($control['blood_pressure']['low']) || isset($control['blood_pressure']['high'])) {
            $print['pressure'] = '血压 ';

            if (isset($control['blood_pressure']['high'])) {
                $print['pressure'] .= $control['blood_pressure']['high'];
            }

            $print['pressure'] .= '/';

            if (isset($control['blood_pressure']['low'])) {
                $print['pressure'] .= $control['blood_pressure']['low'];
            }

            $print['pressure'] .= 'mmHg';
        }

        $print[] = $this->fe('control_objectives', 'ldl-c', 'LDL-C', 'mmol/L');
        $print[] = $this->fe('control_objectives', 'TG', '血清总胆固醇(TG)', 'mmol/L');
        $print[] = $this->fe('control_objectives', 'TC', '甘油三酯(TC)', 'mmol/L');

        return $this->getPrintStr('设定患者控制目标：', $print);
    }

    public function advise()
    {
        $advise = $this->model->advise;

        if (!$advise) {
            return null;
        }

        $print = [];

        $advise = collect($advise)->filter(function ($value, $key) {
            return $value['accepted'] == 'on';
        });

        $text = app('field')->get('dispose.advise.text');

        if ($this->scene->isWestern()) {
            $advise->map(function ($value, $key) use ($text, &$print) {
                if (method_exists($this, $key)) {
                    $print[] = $this->ft('dispose.advise.text', $key). '：' . $this->$key($value);
                } else {
                    $print[] = $this->ft('dispose.advise.text', $key);
                }
            });
        } else {
            collect($text)->map(function ($text, $key) use ($advise, &$print) {
                $value = $advise->get($key);

                if (is_null($value))
                    return '';

                if (method_exists($this, $key)) {
                    $print[] = $text . '：' . $this->$key($value);
                } else {
                    $print[] = $text;
                }

                if ($key == 'lose_weight' && ($control = $this->control())) {
                    $print[] = $control;
                }

                if ($key == 'take_drugs') {
//                    $print[] = $this->medicationFormat($this->model->medication);
//                    $print[] = $this->cnMedicationFormat($this->model->cn_medication);
                }

                if ($key == 'smbg') {
                    $print[] = $this->plan();
                }
            });
        }
        $key = 1;
        $result = [];

        foreach ($print as $value) {
            if (!is_null($value)) {
                $result[$key] = $key . '. ' . mb_trim($value, '：、');
                ++$key;
            }
        }

        return implode('<br>', $result);
    }

    public function inspectionPlan()
    {
        if ($this->scene->isWestern()) {
            return $this->plan();
        }

        return '';
    }

    public function plan()
    {
        $plan = $this->model->inspection_plan;

        if (!$plan) {
            return null;
        }

        $print   = [];
        $dispose = app('field')->get('dispose.inspection_plan');

        collect($plan)->map(function ($plan, $key) use (&$print, $dispose) {

            if (isset($plan['accepted']) && ($plan['accepted'] == 'on')) {
                $print[] = $dispose[$key] . ' 检查周期 ' . $plan['cycle'] . '月';
            }
        });

        if (isset($plan['extend']) && !empty($plan['extend'])) {
            $extend = $plan['extend'];
            unset($plan['extend']);

            collect($extend)->map(function ($plan, $key) use (&$print) {
                $print[] = $plan['title'] . ' 检查周期 ' . $plan['cycle'] . '月';
            });
        }

        return $this->getPrintStr('检查方案：', $print);
    }

    public function schedule()
    {
        $schedule = $this->model->schedule;

        if (!$schedule) {
            return null;
        }

        $print = [
            $this->fe('schedule', 'transfer_treatment', '建议转诊'),
            $this->fe('schedule', 'plan', '治疗方案及处方'),
            $this->fe('schedule', 'date', '日期'),
        ];

        return $this->getPrintStr('', $print);
    }

    public function diet($value)
    {
        if ($this->scene->isWestern()) {
            return isset($value['item']) ? $value['item'] : '';
        }

        $print    = [];
        $print[0] = '';

        if (isset($value['staple'])) {
            $print[0] .= '主食 ' . $value['staple'] . ' 两：';
        }

        if (isset($value['breakfast'])) {
            $print[0] .= '早 ' . $value['breakfast'] . ' 两、';
        }

        if (isset($value['lunch'])) {
            $print[0] .= '午 ' . $value['lunch'] . ' 两、';
        }
        if (isset($value['dinner'])) {
            $print[0] .= '晚 ' . $value['dinner'] . ' 两、';
        }

        if (array_key_exists(0, $print)) {
            $print[0] = mb_trim($print[0], '：、');
            $print[0] .= '(生品、细粮)';
        }

        if (isset($value['meat'])) {
            $print[] = '生肉或淡水鱼< ' . $value['meat'] . ' 两(中午2/3，晚上1/3)';
        }

        $print[] = '禁食：甜食，动物内脏，海产品，煎炸物，坚果，咸菜';
        $print[] = '蔬菜不限量';

        foreach ($print as $key => &$value) {
            $value = '<br>　（' . ($key + 1) . '）' . $value . '；';
        }

        return implode('', $print);
    }

    public function rest_schedule($value)
    {
        if (\Auth::user()->hospital) {
            if (\Auth::user()->hospital->isWestern()) {
                return '';
            }
        }

        return '晚上10:30入睡,早上6:30起床，午休不超过半个小时';
    }

    public function low_salt($value)
    {
        if (\Auth::user()->hospital) {
            if (\Auth::user()->hospital->isWestern()) {
                return '';
            }
        }

        return isset($value['item']) ? '每天 < ' . $value['item'] . '克食盐' : '';
    }

    public function smbg($value)
    {
        $items = array_keys($value['items']);

        $i = Arr::only(app('field')->get('dispose.advise.items.smbg'), $items);

        return $i ? implode('、', $i) : '';
    }

    public function pec($value)
    {
        $i = [];

        if (isset($value['items'])) {
            $i = Arr::only(app('field')->get('dispose.advise.items.pec'), $value['items']);
        }

        if (!empty($value['other'])) {
            $i[] = $value['other'];
        }

        return $i ? implode('、', $i) : '';
    }

    public function regular_exercise($value)
    {

        if (\Auth::user()->hospital) {
            if (\Auth::user()->hospital->isWestern()) {
                return isset($value['item']) ? $value['item'] : '';
            }
        }
        if (isset($value['item']) && !empty($value['item'])) {
            return '三餐后1小时平地散步/快走，每次 ' . $value['item'] . ' 分钟';
        }

        return '';
    }

    public function lose_weight($value)
    {
        if (isset($value['item'])) {
            return $value['item'];
        }

        return '';
    }
}